﻿using MyFileView.Core;
using MyFileView.Core.Core;
using MyFileView.Shard;
using MyFileView.Shard.Model;

using MyFleView.Core;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;

namespace MyFleView.Service
{
    public interface IConvertFileService:RegisterAutofac
    {
        //  void CreateContext(UploadFileModel uploadFileModel);
        string ConvertFilePrepare();
    }

    public class ConvertFileService (IEnumerable<IFilePreview> filePreview, IConvertFileFactory convertFileFactory) : IConvertFileService//AbstractService  
    {
    
        /// <summary>
        /// 转换前准备
        /// </summary>
        /// <param name="uploadFileModel"></param>
        public string ConvertFilePrepare()
        {

            ///创建上下文
            //  _convertFileContext = _fileFactory.CreateContext(convertFileInfo);
            //请求数据对象映射
            //var fileInfo = ConvertFileInfo.Create(uploadFileModel);
            //_convertFileContext = _fileFactory.CreateContext(fileInfo);
            //   var res = _filePreview.FileView();,IConvertFileContext convertFileContext
            var fileInfo = convertFileFactory.ConvertFileContext.FileInfo;
            string action = "Index";
            ///这里会重复，
            foreach (var item in filePreview)
            {
                if (item.Name.Contains(fileInfo.FileSuffix))
                {
                    item.FilePreview();
                    break;
                }
            }
            GenerateResponseData();
            return action;
           ;
         //   _convertFileContext.LibreOfficeDocument.ConvertFile();


        }


        public void GenerateResponseData() 
        {
            // var httpcontext = convertFileFactory.HttpContext;
            //var data = convertFileFactory.ConvertFileContext.FileInfo;
            //var handle = convertFileFactory.ConvertFileContext.AfterConvertPathHandle;
           var PreviewFileResponseModel=   MappingFactoryClass.Mapping(convertFileFactory);
            // responseData.Url=
     
               // responseData.Id = data.Id;
           
            HttpRuntime.Cache.Insert("preview", PreviewFileResponseModel);
           // convertFileFactory.HttpContext.Cache.Insert();
        }

    }
}